//app.js
require('./libs/bootstrap/index')

const request = require("miniapp-request");
App({
    onLaunch: function(e) {
        console.log(e);
        this.globalData.scene = e.scene;
    },
    checkUserAuth() {
        return new Promise((resolve, reject) => {
            let auth = []
            global.loginPromise.then(res => {
                if (!res.phone) {
                    auth.push({
                        auth: 'phone',
                        value: false
                    })
                } else {
                    auth.push({
                        auth: 'phone',
                        value: true
                    })
                }
                wx.getSetting({
                    success(res) {
                        const {
                            authSetting
                        } = res
                        if (authSetting["scope.userInfo"] === undefined ||
                            authSetting["scope.userInfo"] === false) {
                            auth.push({
                                auth: 'userInfo',
                                value: false
                            })
                        } else {
                            auth.push({
                                auth: 'userInfo',
                                value: true
                            })
                        }

                        resolve(auth)
                    }
                })
            })
        })
    },
    authDirect(auth, cb) {
        const firstAuth = auth.find(v => !v.value)
        if (firstAuth) {
            switch (firstAuth.auth) {
                case "phone":
                    wx.navigateTo({
                        url: '/pages/auth/phone'
                    })
                    break
                case "userInfo":
                    wx.navigateTo({
                        url: '/pages/auth/userinfo'
                    })
                    break
            }
        } else {
            if (cb) {
                cb()
            } else {
                const route = getCurrentPages()
                if (route[route.length - 1].route !== 'pages/auth/phone' || route[route.length - 1].route !== 'pages/auth/userinfo') {
                    return
                }
                let delta = 0
                    // let targetRoute = null
                for (let i = route.length - 1; i >= 0; i--) {
                    if (route[i].route !== 'pages/auth/phone' && route[i].route !== 'pages/auth/userinfo') {
                        // targetRoute = route[i]
                        break
                    } else {
                        delta += 1
                    }
                }
                wx.navigateBack({
                    delta
                })
            }
        }
    },
    globalData: {
        scene: '',
        hasHomeAdShow: false,
        httpsDomain: "https://jiankangpao.sichuancancer.org/azfzjkp",
    }
})